package com.javasvup.servicecode.service.impl;

import com.javasvip.common.pojo.AuthCode;
import com.javasvip.common.pojo.service.CodeService;
import com.javasvup.servicecode.dao.AuthCodeDao;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * dubbo 校验验证码服务
 */
@Service
public class CodeServiceImpl implements CodeService {
    @Autowired
    AuthCodeDao authCodeDao;

    @Override
    public Integer verification(String email, String code) {
        System.out.println("===验证：" + email + ":" + code);
        List<AuthCode> authCodes = authCodeDao.queryByEmail(email);
        if (authCodes != null && authCodes.size() > 0) {
            AuthCode authCode = authCodes.get(0);
            System.out.println("===" + authCode);
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date time = new Date();
                Date endTime = sdf1.parse("2020-11-01 16:30:48.0");
                if (time.getTime() > endTime.getTime()) {
                    if (code.equals(authCode.getCode())) {
                        System.out.println("验证成功" + code + ":" + authCode.getCode());

                        return 0;
                    } else {
                        System.out.println("验证失败");
                        return 1;
                    }

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return 1;
        } else {
            return 1;
        }
    }
}
